package com.credit.util;

import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.credit.entity.CreditLoanOrder;
import com.credit.entity.CreditUserInfo;
import com.credit.service.UserService;

import net.sf.json.JSONObject;

public class CalcuationOrderPriceUtil {
	private static final Log log = LogFactory.getLog(CalcuationOrderPriceUtil.class);
	
	/**
	 * 返回订单价格
	 * @param creditLoanOrder
	 * @return
	 */
	public static Integer calcuationPrice(CreditLoanOrder creditLoanOrder){
		
		Integer price = 0;
		
		if(creditLoanOrder.getOrderType()!=CodeValueUtil.ORDERTYPE_TAODAN){ //普通价格
			price = creditLoanOrder.getAmount().multiply(new BigDecimal(0.0015)).intValue();
			
			
			if(creditLoanOrder.getWorkType().intValue()==CodeValueUtil.WORKTYPE_OFFICE.intValue()){
				
				log.info("-------------------------"+creditLoanOrder.getExtAttribute()+"----------------------");
				
				JSONObject jsonObject = JSONObject.fromObject(creditLoanOrder.getExtAttribute());
				if(creditLoanOrder.getInsurance().intValue() == CodeValueUtil.HAVE.intValue()){//社保
					
					if(jsonObject.get("socialYears")!=null&&!"null".equals(jsonObject.get("socialYears").toString())){
						int socialYears = jsonObject.getInt("socialYears");
						if(socialYears>=CodeValueUtil.SOCIALYEARS_3_6_MONTHS.intValue()&&
								socialYears<CodeValueUtil.SOCIALYEARS_6MONTHS_1YEAR.intValue()){ //
							price += 3; 
						}else if(socialYears>=CodeValueUtil.SOCIALYEARS_6MONTHS_1YEAR.intValue()){
							price += 5;
						}
					}
					
				}
				
				if(jsonObject.get("openSalary")!=null&&!"null".equals(jsonObject.get("openSalary").toString())){
					String openSalary = jsonObject.getString("openSalary");
					if(Double.valueOf(openSalary)>3000){ //打卡大于3000
						price += 3; 
					}
				}
			
				
				if(jsonObject.get("companyType")!=null&&!"null".equals(jsonObject.get("companyType").toString())){
					int companyType = jsonObject.getInt("companyType");
					if(companyType==CodeValueUtil.COMPANYTYPE_TOPFIVEHUNDRED.intValue()||
							companyType==CodeValueUtil.COMPANYTYPE_INSTITUTION||
									companyType==CodeValueUtil.COMPANYTYPE_STATECOMPANY||
									companyType==CodeValueUtil.COMPANYTYPE_QUOTEDCOMPANY){//公司性质
						price += 5;
					}
				}
				
				
			}

			
			if((creditLoanOrder.getCreditType()!=null)&&(creditLoanOrder.getCreditType().intValue()==CodeValueUtil.CREDITTYPE_LOAN.intValue())){
				if(creditLoanOrder.getCapital().intValue()==CodeValueUtil.CAPITAL_HOUSE.intValue()){ //房贷
					price += 3;
				}
				
				if(creditLoanOrder.getCreditType().intValue()==CodeValueUtil.CAPITAL_CAR.intValue()){//车贷
					price += 3;
				}
				
				if(creditLoanOrder.getCreditType().intValue()==CodeValueUtil.CAPITAL_HOUSEANDCAR.intValue()){//车和房贷
					price += 6;
				}
			}

			
			UserService userService = (UserService) ApplicationContextUtils.getBean("userService");
			
			CreditUserInfo userInfo = userService.findUserById(creditLoanOrder.getUserId());
			if(userInfo.getSesameCredit().intValue()>=600){ //芝麻信用大于600
				price += 3;
			}
			
			if(userInfo.getIdentifyCheck()!=null&&userInfo.getIdentifyCheck().intValue()==CodeValueUtil.IDENTIFY_CHECK){ //身份认证
				price += 3;
			}
			
			if(price<=15){
				price = 15;
			}
			
			if(price>200){
				price = 200;
			}
		}else{ //淘单价格
			price = creditLoanOrder.getAmount().multiply(new BigDecimal(0.001)).intValue();
			if(price<=15){
				price = 15;
			}
			
			if(price>200){
				price = 200;
			}
		}
		
		return price;
	}
	
	public static void main(String[] args){
		String str = "{\"socialYears\":null}";
		JSONObject json = JSONObject.fromObject(str);
		
		
		
		System.out.println(json.get("socialYears").toString());
		
		System.out.println(json.get("socialYears").toString().equals("null"));
		
		json.getInt("socialYears");
	}

}
